/*
 * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
 * Copyright (C) 2006-2009 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
 * Copyright (C) 2010-2015 Rising Gods <http://www.rising-gods.de/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ScriptMgr.h"
#include "ScriptedCreature.h"
#include "blackrock_spire.h"

enum Spells
{
    SPELL_REND_MOUNTS               = 16167, // Change model
    SPELL_CORROSIVE_ACID            = 16359, // Combat (self cast)
    SPELL_FLAMEBREATH               = 16390, // Combat (Self cast)
    SPELL_FREEZE                    = 16350, // Combat (Self cast)
    SPELL_KNOCK_AWAY                = 10101, // Combat
    SPELL_SUMMON_REND               = 16328  // Summons Rend near death
};

enum Misc
{
    NEFARIUS_PATH_2                 = 1379671,
    NEFARIUS_PATH_3                 = 1379672,
    GYTH_PATH_1                     = 1379681,
};

enum Events
{
    EVENT_CORROSIVE_ACID            = 1,
    EVENT_FREEZE                    = 2,
    EVENT_FLAME_BREATH              = 3,
    EVENT_KNOCK_AWAY                = 4,
    EVENT_SUMMONED_1                = 5,
    EVENT_SUMMONED_2                = 6
};

class boss_gyth : public CreatureScript
{
    public:
        boss_gyth() : CreatureScript("boss_gyth") { }
    
        struct boss_gythAI : public BossAI
        {
            boss_gythAI(Creature* creature) : BossAI(creature, DATA_GYTH) { }
            void Reset() override
            {
                SummonedRend = false;
                if (instance->GetBossState(DATA_GYTH) == IN_PROGRESS)
                {
                    instance->SetBossState(DATA_GYTH, DONE);
                    me->DespawnOrUnsummon();
                }
            }
    
            void EnterCombat(Unit* who) override
            {
                BossAI::EnterCombat(who);
    
                events.ScheduleEvent(EVENT_CORROSIVE_ACID, urand(8, 16) * IN_MILLISECONDS);
                events.ScheduleEvent(EVENT_FREEZE,         urand(8, 16) * IN_MILLISECONDS);
                events.ScheduleEvent(EVENT_FLAME_BREATH,   urand(8, 16) * IN_MILLISECONDS);
                events.ScheduleEvent(EVENT_KNOCK_AWAY,    urand(12, 18) * IN_MILLISECONDS);
            }
    
            void JustDied(Unit* /*killer*/) override
            {
                instance->SetBossState(DATA_GYTH, DONE);
                if (!me->FindNearestCreature(NPC_WARCHIEF_REND_BLACKHAND, 100.0f))
                    DoCastSelf(SPELL_SUMMON_REND, true);
                if (GameObject* gate = me->GetMap()->GetGameObject(instance->GetGuidData(GO_PORTCULLIS_ACTIVE)))
                    gate->SetGoState(GO_STATE_ACTIVE);
            }
    
            void SetData(uint32 /*type*/, uint32 data) override
            {
                switch (data)
                {
                    case 1:
                        events.ScheduleEvent(EVENT_SUMMONED_1, 1 * IN_MILLISECONDS);
                        break;
                    default:
                        break;
                }
            }
    
            void UpdateAI(uint32 diff) override
            {
    
                if (!SummonedRend && HealthBelowPct(5))
                {
                    DoCastSelf(SPELL_SUMMON_REND);
                    me->RemoveAura(SPELL_REND_MOUNTS);
                    SummonedRend = true;
                }
    
                if (!UpdateVictim())
                {
                    events.Update(diff);
    
                    while (uint32 eventId = events.ExecuteEvent())
                    {
                        switch (eventId)
                        {
                            case EVENT_SUMMONED_1:
                                me->AddAura(SPELL_REND_MOUNTS, me);
                                if (GameObject* portcullis = me->FindNearestGameObject(GO_DR_PORTCULLIS, 40.0f))
                                    portcullis->UseDoorOrButton();
                                if (Creature* victor = me->FindNearestCreature(NPC_LORD_VICTOR_NEFARIUS, 75.0f, true))
                                    victor->AI()->SetData(1, 1);
                                events.ScheduleEvent(EVENT_SUMMONED_2, 2 * IN_MILLISECONDS);
                                break;
                            case EVENT_SUMMONED_2:
                                me->GetMotionMaster()->MovePath(GYTH_PATH_1, false);
                                break;
                            default:
                                break;
                        }
                    }
                    return;
                }
    
                events.Update(diff);
    
                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_CORROSIVE_ACID:
                            DoCastSelf(SPELL_CORROSIVE_ACID);
                            events.ScheduleEvent(EVENT_CORROSIVE_ACID, urand(10, 16) * IN_MILLISECONDS);
                            break;
                        case EVENT_FREEZE:
                            DoCastSelf(SPELL_FREEZE);
                            events.ScheduleEvent(EVENT_FREEZE, urand(10, 16) * IN_MILLISECONDS);
                            break;
                        case EVENT_FLAME_BREATH:
                            DoCastSelf(SPELL_FLAMEBREATH);
                            events.ScheduleEvent(EVENT_FLAME_BREATH, urand(10, 16) * IN_MILLISECONDS);
                            break;
                        case EVENT_KNOCK_AWAY:
                            DoCastVictim(SPELL_KNOCK_AWAY);
                            events.ScheduleEvent(EVENT_KNOCK_AWAY, urand(14, 20) * IN_MILLISECONDS);
                            break;
                        default:
                            break;
                    }
                }
                
                DoMeleeAttackIfReady();
            }
            
        private:
            bool SummonedRend;
        };
    
        CreatureAI* GetAI(Creature* creature) const override
        {
            return GetInstanceAI<boss_gythAI>(creature);
        }
};

void AddSC_boss_gyth()
{
    new boss_gyth();
}
